let app = getApp()
Component({
    properties: {
      messageInfo: {
          type:Array,
          observer: function(newVal, oldVal) {
            this.updateRate()
          }
        }
      },
      data:{
        form_setting:[]
      },
    lifetimes: {
        attached: function () {
        
       }
      },
    methods: {
      messagelist:function(e){
        var handlerItem= {
          css: {},
          nosubmit:true,
          list: [ {
              values:e.detail
          } ]
        }
       var form_setting=handlerItem.list[0]['values']
       this.setData({
        form_setting:form_setting,
        handlerItem:handlerItem
       })
      },
      writemeaasge:function(){ 
          var form_setting=this.data.form_setting
          for(var i=0;i<form_setting.length; i++){
            if(form_setting[i]['required']==1&&form_setting[i]['show']==1){
              if(typeof(form_setting[i]['value'])=='undefined'||form_setting[i]['value']==''){
                if(form_setting[i]['type']=='text'||form_setting[i]['type']=='textarea'||form_setting[i]['type']=='phone'||form_setting[i]['type']=='id_card'||form_setting[i]['type']=='car_number'){
                  wx.showToast({
                    title:'请输入'+form_setting[i]['name'],
                    icon: 'none',
                    duration: 1500
                  })
                  return false
                }
                if(form_setting[i]['type']=='radio'||form_setting[i]['type']=='checkbox'){
                  wx.showToast({
                    title:'请选择'+form_setting[i]['name'],
                    icon: 'none',
                    duration: 1500
                  })
                  return false
                }
                if(form_setting[i]['type']=='file'){
                  wx.showToast({
                    title:'请上传文件',
                    icon: 'none',
                    duration: 1500
                  })
                  return false
                }
                if(form_setting[i]['type']=='date'){
                  wx.showToast({
                    title:'请选择日期',
                    icon: 'none',
                    duration: 1500
                  })
                  return false
                }
                if(form_setting[i]['type']=='datetime'){
                  wx.showToast({
                    title:'请选择时间',
                    icon: 'none',
                    duration: 1500
                  })
                  return false
                }
                if(form_setting[i]['type']=='select'){
                  form_setting[i]['default_value']=form_setting[i]['options'][0]['op_name']
                  form_setting[i]['value']=form_setting[i]['options'][0]['op_name']
                  var handlerItem= {
                    css: {},
                    nosubmit:true,
                    list: [ {
                        values:form_setting
                    } ]
                  }
                  this.setData({
                    handlerItem:handlerItem,
                    form_setting:form_setting
                  })
                }
              }
              if(form_setting[i]['type']=='phone'){
                var myreg=/^1\d{10}$/
                 if (!myreg.test(form_setting[i]['value'])) {
                   wx.showToast({
                     title: '请输入正确'+form_setting[i]['name'],
                     icon: 'none',
                     duration: 1500
                   })
                   return false
                 }
               }
               if(form_setting[i]['type']=='id_card'){
                var myreg = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
                 if (!myreg.test(form_setting[i]['value'])) {
                   wx.showToast({
                     title:"请输入正确的身份证号",
                     icon: 'none',
                     duration: 1500
                   })
                   return false
                 }
               }  
            }
          }
          this.triggerEvent('messageinfo',form_setting)
          this.setData({
            visible:false,
            maskShow:false,
            edit:false,
            showvueId:false
          })
      },
     
      cancel:function(){
        this.setData({
          visible:false,
          maskShow:false,
          edit:false,
          showvueId:false
        })
      },
     updateRate:function(){
        var handlerItem= {
          css: {},
          nosubmit:true,
          list: [ {
              values:this.data.messageInfo
          } ]
        }
        var form_setting=handlerItem.list[0]['values']
        if(this.data.messageInfo.length>0){
          this.setData({
            edit:true,
            showvueId:true,
            visible:true,
            maskShow:true,
            form_setting:form_setting,
            handlerItem:handlerItem
          })
        }else{
          this.setData({
            edit:false,
            showvueId:false,
            visible:false,
            maskShow:false,
            form_setting:form_setting,
            handlerItem:handlerItem
          })
        }
       
      }
    }
});
